home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / challenge / 12.09-Sep96 / Testcode96.09R1.sit / Testcode96.09R1 / 1 Bee Swarm R1 / swarm.java < prev    next >
Text File  |  1996-08-19  |  11KB  |  367 lines

  1. /**
  2.  * MODIFIED FROM THE ORIGINAL BY Bob Boonstra for the Programmer's Challenge
  3.  *
  4.  *  Title: BeeSwarm 
  5.  *  Type: Applet
  6.  *  Source: swarm.java
  7.  *  Description: Displays a window with swarming bees chasing a lone queen bee.
  8.  *        The queen bee darts about the screen and the drones follow in her
  9.  *        wake, swarming and buzzing about, changing colors as they go.
  10.  *        
  11.  *  Author: Michael Nosal
  12.  *  Symantec Corporation ©1996
  13.  */
  14. /*****************************************************
  15.  *    import java.awt.*;
  16.  *    import java.lang.*;
  17.  ******************************************************/
  18.  
  19. /** ---------------------------------------------------------------------------------
  20.  *  Class: swarm - manages the swarm of bees and the queen.
  21.  *  We start the queen and the drones at the same location. random jitter to the position of
  22.  *  each bee moves them around the screen. The queen won't go off the edge of the window,
  23.  *  while the bees will. The bees also change color.
  24.  */
  25. public class swarm extends java.applet.Applet /*implements Runnable*/ {
  26.  
  27.     int    numBees,numIters;;
  28.     int queenBeex,queenBeey,queenBeedX,queenBeedY,queenBeeoldX,queenBeeoldY;
  29.     int    dronesX[],dronesY[],dronesDX[],dronesDY[],dronesOldX[],dronesOldY[];
  30. /*****************************************************
  31.  *        Color queenBeebeeColor,dronesbeeColor[];
  32.  *        String sumString;
  33.  ******************************************************/
  34.     int hue;
  35.     int MAXX;
  36.     int MAXY;
  37.     int xMin, xMax, yMin, yMax;
  38.     int MAXBV;        // max bee velocity (pixels per frame)
  39.     int MAXQV;        // max queen velocity (pixels per frame)
  40.     int MAXQA;        // max queen acceleration
  41.     int MAXBA;        // max bee acceleration
  42.     int borderWidth;    // queen won't go nearer than this to the edge of the window
  43.     int myRandSeed;
  44.     int myRandMultiplier;
  45.     int sum;
  46.  
  47. /*****************************************************
  48.  *        // for double buffering to prevent flicker
  49.  *        Image        offScreenImage;
  50.  *        Graphics    offScreen;
  51.  ******************************************************/
  52.   
  53.     int qstartX, qstartY, qstartV;
  54.  
  55.     int b;
  56.  
  57. /** ---------------------------------------------------------------------------------
  58.  *  init() - creates the queen bee and the drones, and trys to set up the offscreen graphics
  59.  *  world for double-buffering the animation.
  60.  */
  61.     void myInit(){
  62.         hue = 270;
  63.         MAXX = 400;
  64.         MAXY = 400;
  65.         xMin = 10000;
  66.         xMax = -10000;
  67.         yMin = 10000;
  68.         yMax = -10000;
  69.         MAXBV = 9;        // max bee velocity (pixels per frame)
  70.         MAXQV = 12;        // max queen velocity (pixels per frame)
  71.         MAXQA = 5;        // max queen acceleration
  72.         MAXBA = 3;        // max bee acceleration
  73.         borderWidth = 20;    // queen won't go nearer than this to the edge of the window
  74.         myRandSeed = 0x01234567;
  75.         myRandMultiplier = 0x77777777;
  76.         dronesX = new int[numBees];
  77.         dronesY = new int[numBees];
  78.         dronesDX = new int[numBees];
  79.         dronesDY = new int[numBees];
  80.         dronesOldX = new int[numBees];
  81.         dronesOldY = new int[numBees];
  82. /*****************************************************
  83.  *            dronesbeeColor = new Color[numBees];
  84.  ******************************************************/
  85.  
  86. /*****************************************************
  87.  *            resize(MAXX,MAXY);
  88.  ******************************************************/
  89.  
  90.         //create the queen bee
  91.         //we want the starting X pos to be within some border of the edge of the window
  92.         qstartX = RangedRdm(borderWidth,MAXX - borderWidth);
  93.         qstartY = RangedRdm(borderWidth,MAXY - borderWidth);
  94.  
  95.  
  96.         queenBeex = qstartX;
  97.         queenBeey = qstartY;
  98.         queenBeedX = 0;
  99.         queenBeedY = 0;
  100.         queenBeeoldX = queenBeex;
  101.         queenBeeoldY = queenBeey;
  102. /*****************************************************
  103.  *            queenBeebeeColor = Color.red;
  104.  ******************************************************/
  105.         // create the new bees
  106.         for (int i=0; i < numBees; i++) {
  107.                     
  108.             dronesX[i] = qstartX;
  109.             dronesY[i] = qstartY;
  110.             dronesDX[i] = RandZip(MAXBA);
  111.             dronesDY[i] = RandZip(MAXBA);
  112.             dronesOldX[i] = dronesX[i];
  113.             dronesOldY[i] = dronesY[i];
  114.         } 
  115.  
  116. /*****************************************************
  117.  *            offScreenImage = createImage (MAXX, MAXY);
  118.  *            offScreen = offScreenImage.getGraphics (); 
  119.  *            offScreen.setColor(Color.black);
  120.  *            offScreen.fillRect(0,0,MAXX,MAXY);
  121.  ******************************************************/
  122.         
  123.     } // end of swarm.init
  124.     
  125.     int myRun()
  126.     {
  127.         numBees = 50;
  128.         numIters = 5000;
  129.         myInit();
  130.         for (int loopCount=0; loopCount<numIters; loopCount++) {
  131.             updateBees();
  132. //            repaint();
  133.         }
  134.         sum = 0;
  135.         for (int b=0; b<numBees; b++) {
  136.             sum += dronesX[b];
  137.             sum += dronesY[b];
  138.         }
  139.         return sum;
  140.     }
  141.  
  142. /** --------------------------------------------------------------------------
  143.  *  start() - initializes a new thread
  144.  *
  145.  */
  146.     public void start() {
  147.         sum = myRun();
  148.         
  149.     } //end of swarm.start
  150.  
  151. /** --------------------------------------------------------------------------
  152.  *  destroy() - we dispose of our offscreen graphics world to free up resources
  153.  *
  154.  */
  155. /*****************************************************
  156.  *        public void destroy() {
  157.  *            if (offScreen != null) {
  158.  *                offScreen.dispose();
  159.  *            }
  160.  *        } // end of destroy
  161.  ******************************************************/
  162.  
  163. /** --------------------------------------------------------------------------
  164. *  paint() - this decides if we can paint on the offscreen graphics world or if we go
  165. *  straight to the screen.
  166. */
  167. /*****************************************************
  168.  *        public void paint(Graphics g)
  169.  *        {
  170.  *            if (offScreen != null) 
  171.  *            {    // double-buffering available
  172.  *                offScreen.setColor(Color.black);
  173.  *                offScreen.fillRect(0,0,MAXX,MAXY);
  174.  *                paintApplet(offScreen);
  175.  *                g.drawImage (offScreenImage, 0, 0, this);
  176.  *                //g.clipRect(0,0,MAXX,MAXY);
  177.  *            }
  178.  *            else 
  179.  *            {    // no double-buffering
  180.  *                g.setColor(Color.black);
  181.  *                g.fillRect(0,0,MAXX,MAXY);
  182.  *                paintApplet (g);
  183.  *             }
  184.  *        } // end of swarm.paint;
  185.  *    
  186.  ******************************************************/
  187. /** --------------------------------------------------------------------------
  188. *  paintApplet() - This makes the actual calls to draw the bees on the screen.
  189. *
  190. */
  191. /** --------------------------------------------------------------------------
  192.  *        public void paintApplet(Graphics g)
  193.  *        {
  194.  *            g.setColor(Color.black);
  195.  *    
  196.  *            g.fillRect(xMin-MAXQV,yMin-MAXQV,(xMax+MAXQV+MAXQV)-xMin,(yMax+MAXQV+MAXQV)-yMin);
  197.  *            g.setColor(queenBeebeeColor);
  198.  *            
  199.  *            g.drawLine(queenBeeoldX, queenBeeoldY, queenBeex, queenBeey);
  200.  *    
  201.  *            g.setColor(dronesbeeColor[0]);
  202.  *            for (b=0; b < numBees; b++)
  203.  *            { 
  204.  *                g.drawLine(dronesOldX[b],dronesOldY[b],dronesX[b],dronesY[b]);
  205.  *            }    
  206.  *            // just for fun, draw a white dot at the bee's head
  207.  *            g.setColor(Color.white);
  208.  *            for (b=0; b < numBees; b++)
  209.  *            { 
  210.  *                g.drawLine(dronesX[b],dronesY[b],dronesX[b] ,dronesY[b]);
  211.  *            }    
  212.  *            sumString = Integer.toString(sum);
  213.  *            g.drawString(sumString,20,20);
  214.  *    
  215.  *        }    // end paintApplet
  216.  ******************************************************/
  217.     
  218.     int myMin(int a,int b)
  219.     {
  220.         if (a<b) return a;
  221.         return b;
  222.     }
  223.     
  224.     int myMax(int a,int b)
  225.     {
  226.         if (a>b) return a;
  227.         return b;
  228.     }
  229. /** --------------------------------------------------------------------------
  230.  *  update() - this handles the computation of the bee's position. We also keep track
  231.  *  of the bounding box around the swarm, so we only have to update as small a 
  232.  *  region of the screen as possible.
  233.  */
  234.     public void updateBees()
  235.     {    // variables for computing bounding rect for swarm    
  236.         xMin = 10000; xMax = -10000; yMin = 10000; yMax = -10000;
  237.         int dtx, dty, dist;        // distances from bee to queen
  238.         
  239.         xMin = myMin(queenBeey,xMin);
  240.         xMax = myMax(queenBeey,xMax);
  241.         yMin = myMin(queenBeey,yMin);
  242.         yMax = myMax(queenBeey,yMax);
  243.     
  244.         queenBeeoldX = queenBeex;
  245.         queenBeeoldY = queenBeey;
  246.  
  247.         queenBeedX += RandZip(MAXQA);
  248.         queenBeedY += RandZip(MAXQA);
  249.         // keep speed limited to maximums
  250.         if (queenBeedX > MAXQV)
  251.             queenBeedX = MAXQV;
  252.         else
  253.             if (queenBeedX < -MAXQV)
  254.                 queenBeedX = -MAXQV;
  255.  
  256.         if (queenBeedY > MAXQV)
  257.             queenBeedY = MAXQV;
  258.         else
  259.             if (queenBeedY < -MAXQV)
  260.                 queenBeedY = -MAXQV;
  261.  
  262.         // update the queen's position
  263.         queenBeex += queenBeedX;
  264.         queenBeey += queenBeedY;
  265.  
  266.         
  267.         // check to see if the queen hit the edge
  268.         if (queenBeex < borderWidth || queenBeex > (MAXX - borderWidth))
  269.         {
  270.             queenBeedX = -queenBeedX;
  271.             queenBeex += (queenBeedX );
  272.         }
  273.         if (queenBeey < borderWidth || queenBeey > (MAXY - borderWidth))
  274.         {
  275.             queenBeedY = -queenBeedY;
  276.             queenBeey += (queenBeedY );
  277.         }
  278.         xMin = myMin(queenBeex,xMin);
  279.         xMax = myMax(queenBeex,xMax);
  280.         xMin = myMin(queenBeeoldX,xMin);
  281.         xMax = myMax(queenBeeoldX,xMax);
  282.         
  283.         yMin = myMin(queenBeey,yMin);
  284.         yMax = myMax(queenBeey,yMax);
  285.         yMin = myMin(queenBeeoldY,yMin);
  286.         yMax = myMax(queenBeeoldY,yMax);
  287.         // we do color cycling for the bees here
  288.         hue++;
  289.         // for now, all drones have the same color, so we're only setting the color of the first one.
  290. /*****************************************************
  291.  *            dronesbeeColor[0] = Color.getHSBColor((float)(hue/ 360.0),(float)1.0,(float)1.0);
  292.  ******************************************************/
  293.         hue = hue % 359;    // keep going around the color wheel.
  294.         
  295.         for (b=0; b < numBees; b++)
  296.         {     //accelerate
  297.             dtx = queenBeex - dronesX[b];
  298.             dty = queenBeey - dronesY[b];
  299.             dist = Math.abs(dtx) + Math.abs(dty);
  300.             if (dist == 0) 
  301.                 dist = 1;        // avoid dividing by zero
  302.             dronesOldX[b] = dronesX[b];
  303.             dronesOldY[b] = dronesY[b];
  304.             // the randZip adds some extra jitter to prevent the bees from flying in formation
  305.             dronesDX[b] += (int)((dtx * MAXBA )/dist) + RandZip(2);
  306.             dronesDY[b] += (int)((dty * MAXBA )/dist) + RandZip(2);
  307.  
  308.             if (dronesDX[b] > MAXBV)
  309.                 dronesDX[b] = MAXBV;
  310.             else
  311.                 if (dronesDX[b] < -MAXBV)
  312.                     dronesDX[b] = -MAXBV;
  313.  
  314.             if (dronesDY[b] > MAXBV)
  315.                 dronesDY[b] = MAXBV;
  316.             else
  317.                 if (dronesDY[b] < -MAXBV)
  318.                     dronesDY[b] = -MAXBV;
  319.             
  320.             dronesX[b] += dronesDX[b];
  321.             dronesY[b] += dronesDY[b];
  322.             xMin = myMin(dronesX[b],xMin);
  323.             xMax = myMax(dronesX[b],xMax);
  324.             xMin = myMin(dronesOldX[b],xMin);
  325.             xMax = myMax(dronesOldX[b],xMax);
  326.             
  327.             yMin = myMin(dronesY[b],yMin);
  328.             yMax = myMax(dronesY[b],yMax);
  329.             yMin = myMin(dronesOldY[b],yMin);
  330.             yMax = myMax(dronesOldY[b],yMax);
  331.         } // end for b=0 to numBees    
  332.     }
  333.  
  334. /*****************************************************
  335.  *        public void update (Graphics g)
  336.  *        {
  337.  *            updateBees();
  338.  *            paint(g);    
  339.  *        }         
  340.  ******************************************************/
  341.  
  342. /** --------------------------------------------------------------------------
  343.  *  RangedRdm - return a random number in the range min <-> max 
  344.  *
  345.  */
  346.     public int RangedRdm(int min, int max)
  347.     {
  348.         int range, t;
  349.         range = max - min +1;
  350.         t = myRandSeed % range;
  351.         if (t+min > max) 
  352.             t = max - min;
  353.         myRandSeed *= myRandMultiplier;
  354.         myRandSeed &= 0x0000FFFF;
  355.         return (t+min);
  356.     }
  357.     
  358. /** --------------------------------------------------------------------------
  359.  *  RandZip - return a value from -val to +val
  360.  *
  361.  */
  362.     public int RandZip(int val)
  363.     {
  364.         return (RangedRdm(-(val)/2, (val)/2));
  365.     }
  366.  
  367. }